#ifndef WIDGETSCENETREE_H
#define WIDGETSCENETREE_H

#include <QWidget>
#include <QTreeWidgetItem>
#include <QContextMenuEvent>
#include "../entity/EntitySceneNode.h"

namespace Ui {
class WidgetSceneTree;
}

class WidgetSceneTree : public QWidget
{
    Q_OBJECT

public:
    explicit WidgetSceneTree(QWidget *parent = nullptr);
    ~WidgetSceneTree();

    /**
     * @brief 初始化场景树控件
     */
    void Initialize();
    /**
     * @brief 重新加载场景节点
     */
    void Reload();
    /**
     * @brief 新增或修改场景节点
     * @param node
     */
    void EditNode(EntitySceneNode* node);

signals:
    void sig_createNode(const QString& type, EntitySceneNode* parent);
    void sig_selectNode(const QString& name);
    void sig_editNode(const QString& name);
    void sig_nodeTransform(const QString& name);
    void sig_nodeMaterial(const QString& name);
    void sig_nodePhysical(const QString& name);
    void sig_nodeAttachment(const QString& name);
    void sig_nodeAnimation(const QString& name);

protected:
    void dragEnterEvent(QDragEnterEvent *event) override;
    void dropEvent(QDropEvent *event) override;

private slots:
    void on_treeWidget_customContextMenuRequested(const QPoint &pos);

    void treeWidget_itemPressed(QTreeWidgetItem *item);
    void treeWidget_itemDoubleClicked(QTreeWidgetItem *item);

    void actionNew_triggered();
    void actionEdit_triggered();
    void actionVisible_triggered();
    void actionDelete_triggered();
    void actionOperate_triggered();

private:
    void insertNode(QTreeWidgetItem* item, std::shared_ptr<EntitySceneNode> sceneNode);
    void deleteNode(QTreeWidgetItem* item);
    QIcon iconNodeType(const QString& type, bool visible = true);

    static int handleMenu(int, const char** params, void* widgetSceneTree);

private:
    Ui::WidgetSceneTree *ui;
    QMenu* _contextMenu;
    QAction* _actionNew;
    QAction* _actionEdit;
    QAction* _actionVisible;
    QAction* _actionTransform;
    QAction* _actionMaterial;
    QAction* _actionPhysical;
    QAction* _actionDelete;
    QAction* _actionAttach;
    QAction* _actionAnimation;
    QAction* _actionCopyName;
    QAction* _actionNewExt;
};

#endif // WIDGETSCENETREE_H
